
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.chatapp.Databaseacces;

import org.xml.sax.*;             
import org.xml.sax.helpers.*;   
import java.io.*;                 
import java.util.*; 
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 *
 * @author maggiesd
 */
public class PhotoHandler extends org.xml.sax.helpers.DefaultHandler{
    
    private Photo photo;
    private StringBuffer accumulator;
        
    
    public PhotoHandler () {
	photo = new Photo();
    }

    public PhotoHandler (Photo ph) {
	photo = ph;
    }
    
    // Called at the beginning
    public void startDocument() {
	accumulator = new StringBuffer();
    }

    // When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
    // the text in a string buffer. Note that this method may be called in multiple times.
    public void characters(char[] buffer, int start, int length) {
	accumulator.append(buffer, start, length);
    }

    // At the beginning of each new element, erase any accumulated text
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
	accumulator.setLength(0);
    }

    // Take special action when reach the end of selected elements. 
    // In this case the accumulated text is stored in the photo object
    public void endElement(String namespaceURL, String localName, String qname)
    {
	if(localName.equals("title")){
            String title = accumulator.toString().trim();
            photo.setTitle(title);
        }
        else if(localName.equals("creator")){
            String title = accumulator.toString().trim();
            photo.setCreator(title);
        }
        else if(localName.equals("path")){
            String path = accumulator.toString().trim();
            photo.setPath(path);
        }
        else if(localName.equals("ID")){
            String id = accumulator.toString().trim();
            photo.setId(id);
        }
        else if(localName.equals("location")){
            String location = accumulator.toString().trim();
            photo.setLocation(location);
        }
        else if(localName.equals("date")){
            String date = accumulator.toString().trim();
            photo.setDate(date);
        }
        else if(localName.equals("format")){
            String format = accumulator.toString().trim();
            photo.setFormat(format);
        }
        else if(localName.equals("sizeH")){
            String sizeH = accumulator.toString().trim();
            photo.setSizeH(sizeH);
        }
        else if(localName.equals("sizeW")){
            String sizeW = accumulator.toString().trim();
            photo.setSizeW(sizeW);
        }
        else if (localName.equals("keyword")) {        
            String freeTags = accumulator.toString().trim();
            photo.setFreeTags(freeTags);
	}
        else if (localName.equals("tag")) {        
            String taxonomyTags = accumulator.toString().trim();
            photo.setTaxonomyTags(taxonomyTags);
	}
    }

    // Called at the end
    public void endDocument() {
    }

    public void warning(SAXParseException exception) {
	System.err.println("WARNING: line " + exception.getLineNumber() + ": "+
			   exception.getMessage());
    }

    public void error(SAXParseException exception) {
	System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
    }

    public void fatalError(SAXParseException exception) throws SAXException {
	System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
			   exception.getMessage());
	throw(exception);
    }
    
}
